<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of estatisticas
 *
 * @author root
 */
class Operacoes {
    

    public static function getSemestreAtual(){
        if(date('m') <= 6) return 1;
        else return 2;
        
    }
    
    public static function getNumParcSem($sem, $ano){
        Zend_Loader::loadClass('dominio','library');
        $datamat = dominio::getDataMatricula($ano);
        $hoje = date('Y-m-d');
        $mes = date('m');
        $numparc = 0;
        if(strtotime($hoje) <= strtotime($datamat[$sem]['data'])) // ate o vencimento
        {
            $numparc = dominio::QtdPagParcSem($sem);
        }
        else{ // vencido

            if($sem == dominio::TipoSemestreDmn('Anual')){ // anual
                $numparc = dominio::QtdPagParcSem($sem) - $mes;
            }
            else // semestral
            {
                if($mes >= $datamat[$sem]['m']) $mes = $mes - $datamat[$sem]['m'];
                $numparc = dominio::QtdPagParcSem($sem) - $mes;
            }
            
            // passou da data de matricula padrão
            if($datamat[$sem]['m'] == date('m') && $datamat[$sem]['d'] < date('d')) $numparc = $numparc - 1;
            
        }
        
        return $numparc;
    }
    
    static function vagasCurso($id = 0){
        if($id > 0) {
            
            $db = Zend_Db_Table_Abstract::getDefaultAdapter();
            
            $query = 'SELECT COUNT(*) AS TOTAL FROM MATRICULA WHERE MAT_STA = 1 AND CUR_ID = '.$id; 
            $result = $db->fetchRow($query);
            
            $query = 'SELECT SQL_CACHE CUR_NMRMAXALU FROM CURSO WHERE CUR_ID = '.$id; 
            $r_cur = $db->fetchRow($query);
            
            return $r_cur['CUR_NMRMAXALU'] - $result['TOTAL'];     
        }
    }
    
    static function matriculasCurso($id = 0){
        if($id > 0) {
            
            $db = Zend_Db_Table_Abstract::getDefaultAdapter();
            
            $query = 'SELECT COUNT(*) AS TOTAL FROM MATRICULA WHERE MAT_STA = 1 AND CUR_ID = '.$id; 
            $result = $db->fetchRow($query);
            
            return $result['TOTAL'];   
        }
    }
    
}

?>
